<?php
namespace app\shop\model;

use think\Db;
use app\crm\model\Sn;
use app\crm\model\Staff as StaffModel;
use app\shop\model\Client as ClientModel;
use app\shop\model\Clientbill as ClientbillModel;
use app\shop\model\ClientStaff as ClientStaffModel;
use app\shop\model\ClientCategory as ClientCategoryModel;
use app\shop\model\ClientLog as ClientLogModel;

class Linkman extends \ShopModel{

    //列表新增联系人
    public function saveLinkman($params){

        $utid = \request()->header('userId');
        $staff = Db::name('crm_staff')->where('staff_id',$utid)->value('staff_name');

        $params['create_staff'] = $staff;
        $params['update_staff'] = $staff;
        $params['last_contract_staff'] = $staff;
        $params['create_at'] = date('Y-m-d H:i:s');
        $params['update_at'] = date('Y-m-d H:i:s');
        $params['last_contract_time'] = date('Y-m-d H:i:s');

        //获取staff_id
        $SN  = new Sn();
        $snS = $SN->getSn('YG');
        $params['staff_id'] = $snS['0'];

        //登录账号
        $params['user_name'] = $params['staff_name'];
        $params['salt'] = $this->generateSalt();
        $params['password'] = md5(md5('123456').$params['salt']);

        $StaffModel = new StaffModel();

        $StaffModel->data($params)->allowField(true)->save();
        
        $staff_id = $StaffModel->staff_id;

        $crm_index = new \app\crmindex\controller\Staff();
        $crm_index->addxy($staff_id,$params['staff_name'],$params['phone'],$params['duty']);
        
        //标签
        if(!empty($params['tag'])){
            foreach($params['tag'] as $t){
                $tag_tmp = [
                    'tag_id' => $t,
                    'staff_id' => $staff_id
                ];
                Db::name('crm_staff_tag_relevance')->insert($tag_tmp);
            }   
        }

        //直接新增的客户
        if(!empty($params['add_client'])){
            foreach($params['add_client'] as &$a){

                $ClientModel = new ClientModel();

                $a['area'] = !empty($a['area']) ? implode(',', $a['area']) : null;
                $a['create_staff_id'] = $utid;
                $a['create_staff'] = $staff;
                $a['update_staff'] = $staff;
                $a['last_contract_staff'] = $staff;
                $a['last_contract_time'] = date('Y-m-d H:i:s');
                $a['create_at'] = date('Y-m-d H:i:s');
                $a['update_at'] = date('Y-m-d H:i:s');

                $ClientModel->data($a)->allowField(true)->isUpdate(false)->save();
                
                $tmp_client_id = $ClientModel->client_id;
                $a['client_id'] = $ClientModel->client_id;

                //保存客户分类
                $clientCategoryModel = new ClientCategoryModel();
                $clientCategoryDb = $clientCategoryModel->ClientCategoryDb();
                foreach($a['category'] as $v){
                    $tmp = ['client_id'=>$tmp_client_id,'client_cate_id'=>$v];
                    $clientCategoryDb->insert($tmp);
                }

                //保存客户财务
                $ClientbillModel = new ClientbillModel();
                $ClientbillModel->data($a)->allowField(true)->isUpdate(false)->save();

                //保存客户操作日志
                $ClientLogModel = new ClientLogModel();
                $ClientLogDb = $ClientLogModel->ClientLogDb();
                $log = [
                    'client_id' => $tmp_client_id,
                    'operater' => $staff,
                    'content' => '创建客户'
                ];
                $ClientLogDb->insert($log);

                //添加关联
                Db::name('client_staff_relevance')->insert(['client_id'=>$tmp_client_id,'staff_id'=>$staff_id]);
            }
        }

        //关联现有的客户
        if(!empty($params['relation_client'])){
            foreach($params['relation_client'] as $r){
                Db::name('client_staff_relevance')->insert(['client_id'=>$r['client_id'],'staff_id'=>$staff_id]);
            }
        }

        return true;

    }

    /**
     * 生成密码盐
     */
    public function generateSalt()
    {
        $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
        $strMaxIndex = strlen($str) - 1;
        $salt = '';
        $saltLenght = 6;
        for ($i = 0; $i < $saltLenght; $i ++) {
            $salt.= $str[mt_rand(0, $strMaxIndex)];
        }
        return $salt;
    }

}